// FileName: Request.ts
// ProjectName: miu-club
// 作者：区区电脑
// createTime: 2019/9/16

import Axios from 'axios';
import {Dialog} from "@/common/Common";
import UserModel from "@/views/user-center/model/UserModel";

/**
 * 发布状态
 */
export enum ProductState {
    // dev 开发者模式, test 测试模式, product 产品模式
    dev = "dev",
    test = "test",
    product = "product"
}

/**
 * 请求地址
 */
enum RequestHost {
    // dev 开发地址, test 测试地址, product 正式地址
    dev = "/api",
    test = "https://",
    product = "https://"
}

export class RequestPath {
    public getInfo:any = { type: "post", path: "/user/getUserStatus" };
    public upload: any = { type: "post", path: "/webUploadImg" };
}

export class RequestResult {
    public success!: boolean;
    public message: string = "";
    public data: any;
}

export default class Request extends Dialog {
    public name:string = "哈哈哈";
    public productState: ProductState = ProductState.dev;
    public get host(): RequestHost {
        if (this.productState === ProductState.dev) {
            return RequestHost.dev;
        } else if (this.productState === ProductState.test) {
            return RequestHost.test;
        } else {
            return RequestHost.product;
        }
    }
    public start(options: any):Promise<RequestResult> {
        let __this = this;
        let result:RequestResult = new RequestResult();
        return new Promise((resolve, reject) => {
            if (options.loadingMsg) {
                __this.loading.open(options.loadingMsg);
            }
            Axios({
                method: options.path.type,
                url: __this.host + options.path.path,
                data: options.params
            }).then((r) => {
                if (options.loadingMsg) {
                    __this.loading.close();
                }
                if (r.data.code === 0 || r.data.code === "000") {
                    result.success = true;
                    result.message = r.data.message;
                    result.data = r.data.result;
                } else {
                    __this.toast({
                        mes: r.data.message,
                        icon: 'error',
                        callback: () => {}
                    });
                    result.success = false;
                    result.message = r.data.message;
                    result.data = r.data.result;
                }
                resolve(result);
            }).catch((e) => {
                if (options.loadingMsg) {
                    __this.loading.close();
                }
                __this.toast({
                    mes: e,
                    icon: 'error',
                    callback: () => {}
                });
                // result.message = '网络错误，请稍后再试';
                result.success = false;
                resolve(result);
            });
        });
    }
    public fileUpload(): Promise<RequestResult> {
        let __this = this;
        let tempId = parseInt(String(Math.random() * 1000000000));
        let input = document.createElement('input');
        let form = document.createElement('form');
        input.type = 'file';
        input.name = 'file';
        input.id = String(tempId);
        form.style.display = 'none';
        form.id = 'form_' + tempId;
        form.enctype = 'multipart/form-data';
        form.appendChild(input);
        form.style.width = '1px';
        form.style.height = '1px';
        form.style.display = 'none';
        document.body.appendChild(form);
        return new Promise((resolve, reject) => {
            input.onchange = (e) => {
                let formData = new FormData(form);
                let reader = new FileReader();
                // @ts-ignore
                reader.readAsDataURL(input.files[0]);
                reader.onload = (result) => {
                    __this.start({
                        path: new RequestPath().upload,
                        params: formData,
                        loadingMsg: "正在上传..."
                    }).then((r: RequestResult) => {
                        resolve(r);
                    }).catch((e: RequestResult) => {
                        reject(e);
                    })
                };
                form.remove();
            };
            input.click();
        });
    }
}

